<?php

    require_once(dirname(__FILE__)."/../global_conf.php");

     $queries['user_table']="CREATE TABLE user(
                                                id INTEGER PRIMARY KEY AUTO_INCREMENT,
                                                usr VARCHAR(64) NOT NULL,
                                                passwd VARCHAR(32) NOT NULL,
                                                role ENUM('all', 'publish', 'insert') NOT NULL DEFAULT 'insert',
                                                preferred_lang VARCHAR(3) NOT NULL
                                                )ENGINE=INNODB;";
     $queries['content_type_table']="CREATE TABLE node_type(
                                                                 name VARCHAR(128) PRIMARY KEY,
                                                                 complete_name VARCHAR(256) NOT NULL
                                                                 )ENGINE=INNODB;";
     $queries['core_table']="CREATE TABLE core(
                                                id INTEGER PRIMARY KEY AUTO_INCREMENT,
                                                title VARCHAR(256) NOT NULL,
                                                description TEXT NOT NULL,
                                                inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
                                                published BOOL NOT NULL DEFAULT FALSE,
                                                author INTEGER REFERENCES user(id),
                                                content_type VARCHAR(128) REFERENCES node_type(name)
                                                )ENGINE=INNODB;";
     $queries['attribute_definition_table']="CREATE TABLE attribute_definition(
                                                                                id INTEGER PRIMARY KEY AUTO_INCREMENT,
                                                                                short_name VARCHAR(128) UNIQUE,
                                                                                full_name VARCHAR(256) NOT NULL,
                                                                                type VARCHAR(32) NOT NULL
                                                                                )ENGINE=INNODB;";

    function detect_mysql() {
        return function_exists("mysql_connect");
    }

    function create_db_user($credentials) {
        $mysql=new mysqli('localhost', $credentials['dbusr'], $credentials['dbpasswd']);
        if ($mysql->connect_error)
                die($db->connect_error);
        if (!$mysql->query("CREATE DATABASE ".$credentials['newdb']))
                die($mysql->error);
        if (!$mysql->query("CREATE USER ".$credentials['newusr']."@'localhost' IDENTIFIED BY '".$credentials['newpasswd']."'"))
                die($mysql->error);
        if (!$mysql->query("USE ".$credentials['newdb']))
                die("Cannot use database");
        if (!$mysql->query("GRANT ALL PRIVILEGES ON ".$credentials['newdb'].".* TO '".$credentials['newusr']."'@'localhost'"))
                die($mysql->error);
        $mysql->close();

        return true;
    }

    function write_config_php($pwd, $info) {
        $output="<?php".PHP_EOL;
        $output.="\$url=\"".$info['url']."\";".PHP_EOL;
        $output.="\$usr=\"".$info['newusr']."\";".PHP_EOL;
        $output.="\$passwd=\"".$info['newpasswd']."\";".PHP_EOL;
        $output.="\$dbname=\"".$info['newdb']."\";".PHP_EOL;
        $output.="?>";
        $file=fopen($pwd."/config.php", "w");
        if ($file) {
            fwrite($file, $output);
            fclose($file);
            chmod($pwd."/config.php", 0777);

            return true;
        }
        
        return false;
    }

    function create_core_system($admin_usr, $admin_passwd) {
        global $queries, $site;

        foreach ($queries as $query) {
            DBmanager::getInstance()->query($query);
        }
        $query="INSERT INTO user(usr, passwd, role, preferred_lang) VALUE(
                                                                        '".$admin_usr."',
                                                                        MD5('".$admin_passwd."'),
                                                                        'all',
                                                                        'eng'
                                                                        );";
        DBmanager::getInstance()->query($query);
    }
?>